package cn.pw.test.com.conversion.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import cn.pw.test.com.conversion.R;
import cn.pw.test.com.conversion.view.ConversionInputView;
import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.ToastUtil;

/**
 * 黄金换算
 */
public class ConversionGoldFragment extends BaseFragment {

    private ConversionInputView inOuterPrice;
    private ConversionInputView inInsidePrice;
    private ConversionInputView inResult1;
    private ConversionInputView inResult2;

    private TextView calculationTx1, clearTx1, calculationTx2, clearTx2;

    public static ConversionGoldFragment newInstance() {
        ConversionGoldFragment fragment = new ConversionGoldFragment();
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_conversion_gold;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        inOuterPrice = mContentView.findViewById(R.id.conversion_input_outer_price);
        inInsidePrice = mContentView.findViewById(R.id.conversion_input_inside_price);
        inResult1 = mContentView.findViewById(R.id.conversion_input_result1);
        inResult2 = mContentView.findViewById(R.id.conversion_input_result2);
        calculationTx1 = mContentView.findViewById(R.id.button_view_1).findViewById(R.id.conversion_button_positive);
        clearTx1 = mContentView.findViewById(R.id.button_view_1).findViewById(R.id.conversion_button_negative);
        calculationTx2 = mContentView.findViewById(R.id.button_view_2).findViewById(R.id.conversion_button_positive);
        clearTx2 = mContentView.findViewById(R.id.button_view_2).findViewById(R.id.conversion_button_negative);

        calculationTx1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculationOuter();
            }
        });
        calculationTx2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculationInside();
            }
        });

        clearTx1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cleanUp(inOuterPrice, inResult1);
            }
        });
        clearTx2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cleanUp(inInsidePrice, inResult2);
            }
        });
    }

    private void calculationOuter() {
        String outerPrice = inOuterPrice.getInputEd().getText().toString().trim();
        if (DataUtils.isEmpty(outerPrice)) {
            ToastUtil.show(R.string.conversion_string_input_price_hint);
            return;
        }
        double price = Double.valueOf(outerPrice);

        Double result = price / 31.1035 * 6.85;

        inResult1.getInputEd().setText(DataUtils.getAmountValue(result));
    }

    private void calculationInside() {
        String outerPrice = inInsidePrice.getInputEd().getText().toString().trim();
        if (DataUtils.isEmpty(outerPrice)) {
            ToastUtil.show(R.string.conversion_string_input_price_hint);
            return;
        }
        double price = Double.valueOf(outerPrice);

        Double result = price * 31.1035 / 6.85;

        inResult2.getInputEd().setText(DataUtils.getAmountValue(result));
    }

    private void cleanUp(ConversionInputView... view) {
        for (ConversionInputView conversionInputView : view) {
            conversionInputView.getInputEd().setText("");
        }
    }
}
